package com.leandro.tweet;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;


public class Transito extends Activity implements OnClickListener{
	Button bConsultar;
	ImageButton ibOk;
	ImageButton ibNotOk;
	EditText etDigiteRua;
	TextView tvResposta;
	CheckBox autorNConfiavel;
	Context context;
	String host;
	String userId;
	TwitterHelper twitterHelper;
	TweetProjectApplication tweetProject;

	public Transito()
	{
		context = this;
	}

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		tweetProject = (TweetProjectApplication) getApplication();
		twitterHelper = tweetProject.getTwitterHelper();
		userId = twitterHelper.getLoggedUser()+"";
		
		setContentView(R.layout.transito);
		bConsultar = (Button) findViewById(R.id.buttonConsultar);
		bConsultar.setOnClickListener(this);
		etDigiteRua = (EditText) findViewById(R.id.etDigiteRua);
		tvResposta = (TextView) findViewById(R.id.tvResposta);
		ibOk = (ImageButton) findViewById(R.id.ibOk);
		ibNotOk = (ImageButton) findViewById(R.id.ibNotOk);
		ibOk.setOnClickListener(this);
		ibNotOk.setOnClickListener(this);
		autorNConfiavel = (CheckBox) findViewById(R.id.autorConfiavel);
		host = "192.168.0.107";
		//host = "146.164.3.27";
	}
	
	private class FeedBackServer extends AsyncTask<String, String, String>
	{
		String sentence;
		String resposta;
		@Override
		protected String doInBackground(String... dados)
		{
			try
			{

				InetAddress inteAddress = InetAddress.getByName(host);
				SocketAddress socketAddress = new InetSocketAddress(inteAddress, 6788);
				Socket clientSocket = new Socket();
				clientSocket.setSoTimeout(2000);
				clientSocket.connect(socketAddress);
				DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
				outToServer.writeUTF("feedback");
				for (int i=0; i<dados.length; i++)
				{
					sentence = dados[i];
					outToServer.writeUTF(sentence);
				}
				DataInputStream inFromServer = new DataInputStream(clientSocket.getInputStream());
				resposta = inFromServer.readUTF();
				clientSocket.close();

				return resposta;
			}
			catch (Exception e)
			{
				Log.v("Projeto Final", e.toString());

				return "Problema ao conectar ao servidor.";
			}

		}
		
		@Override
		protected void onPostExecute(String result)
		{
			super.onPostExecute(result);
			tvResposta.setText(result);
		}
	}
	
	private class ConsultaServer extends AsyncTask<String, String, String>
	{
		
		
		@Override
		// essa funcao roda em um thread separada;
		protected String doInBackground(String... clientSentence)
		{
			String sentence;
			String modifiedSentence = "";
			try
			{
				InetAddress inteAddress = InetAddress.getByName(host);
				SocketAddress socketAddress = new InetSocketAddress(inteAddress, 6788);
				Socket clientSocket = new Socket();
				clientSocket.setSoTimeout(2000);
				clientSocket.connect(socketAddress);
				DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
				sentence = etDigiteRua.getText().toString();
				
				if(autorNConfiavel.isChecked())outToServer.writeUTF("consulta-nao-confiavel");
				else						   outToServer.writeUTF("consulta-confiavel");
				
				outToServer.writeUTF(sentence);
				DataInputStream inFromServer = new DataInputStream(clientSocket.getInputStream());
				modifiedSentence = inFromServer.readUTF();
				clientSocket.close();

				return modifiedSentence;
			}
			catch (Exception e)
			{
				Log.v("Projeto Final", e.toString());

				return "Problema ao conectar ao servidor.";
			}

		}

		@Override
		protected void onPostExecute(String result)
		{
			super.onPostExecute(result);
			tvResposta.setText(result);
			bConsultar.setEnabled(true);
			ibOk.setVisibility(Button.VISIBLE);
			ibNotOk.setVisibility(Button.VISIBLE);	
		}
	}

	public void onClick(View v) 
	{
		switch(v.getId())
		{
			case R.id.ibOk:
				ibOk.setVisibility(Button.INVISIBLE);
				ibNotOk.setVisibility(Button.INVISIBLE);
				new FeedBackServer().execute(userId, etDigiteRua.getText().toString(),  "Ok");
				break;
			case R.id.ibNotOk:
				ibOk.setVisibility(Button.INVISIBLE);
				ibNotOk.setVisibility(Button.INVISIBLE);
				new FeedBackServer().execute(userId, etDigiteRua.getText().toString(),  "notOk");
				break;
			case R.id.buttonConsultar: 
					bConsultar.setEnabled(false);
					tvResposta.setText("");
					new ConsultaServer().execute();
					break;
		}

	}

}
